/*
 * PlayArea.java
 * Created on July 25, 2008
 * Written by: Clinton Nolan
 *
 * The real main class of the program, contains the graphics and control code
 */

import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.event.MouseInputListener;
import java.util.*;
import ScreenSystem.*;
import MenuSystem.GameMenu;
import MenuSystem.GameMenuItem;
import MenuSystem.ControlNode;
import MenuSystem.KeyDef;
import LuaSystem.LuaEngine;
import MenuSystem.DisplayLuaText;
import MenuSystem.DisplayStringImage;
import MenuSystem.DisplayLuaImage;
import ImageSystem.ImageRegistry;

public class PlayArea extends JComponent{
    private static JFrame parentFrame;             //the frame that contains the window
    private static BufferedImage bimg;             //the screen, used for double buffering and scaling
    private static KeyControl keycontroller;       //the KeyControl, used to remove the component
    private static MouseControl mousecontroller;   //the MouseControl, used to remove the component
    
    /** Creates a new instance of BattleScreen */
    public PlayArea(JFrame f){
        //makes the panel opaque, and thus visible
        this.setOpaque(true);
        
        //KeyControl is registered with the parent frame
        keycontroller = new KeyControl(this);
        f.addKeyListener(keycontroller);
        //MouseControl is registered with the parent frame
        mousecontroller = new MouseControl();
        f.addMouseListener(mousecontroller);
        f.addMouseMotionListener(mousecontroller);
        parentFrame = f;
    }
    
    //called in response to this.repaint();
    public void paintComponent(Graphics g) {
        //clears the background
        super.paintComponent(g);
        
        //converts to Graphics2D
        Dimension d = getSize();
        Graphics2D g2 = createGraphics2D(d.width, d.height);
        //g2.scale(scale,scale);
        
        drawScreen(g2);
        g2.dispose();
        g.drawImage(bimg, 0, 0, this);
    }
    
    //tells the GUI what size the window is
    public Dimension getPreferredSize(){
        return new Dimension(Options.xres,Options.yres);
    }
    
    //makes a Graphics2D object of the given size
    public Graphics2D createGraphics2D(int w, int h) {
        Graphics2D g2 = null;
        if (bimg == null || bimg.getWidth() != w || bimg.getHeight() != h) {
            bimg = (BufferedImage) createImage(w, h);
        }
        g2 = bimg.createGraphics();
        g2.setBackground(getBackground());
        g2.clearRect(0, 0, w, h);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        return g2;
    }
    
    //Draws the screen
    public void drawScreen(Graphics2D g){
        drawBackground(g);
        //drawScreenStack(g);
        
        this.repaint();
    }
    
    //draws the background
    public void drawBackground(Graphics2D g){
        //draw a green background
        g.setColor(Color.black);
        Dimension d = getSize();
        g.fillRect(0,0,d.width,d.height);
    }
    
    public void drawScreenStack(Graphics2D g){
        LinkedList<Screen> ss = ScreenStack.getStack();
        Iterator<Screen> li = ss.descendingIterator();
        while(li.hasNext()){
            Screen current = li.next();
            current.draw(g,this);
        }
    }
    
    //remove the component from the frame
    //useful for replacing this PlayArea with a different one
    public void removeFromFrame(){
        parentFrame.getContentPane().remove(this);
        parentFrame.removeKeyListener(keycontroller);
        parentFrame.removeMouseListener(mousecontroller);
        parentFrame.removeMouseMotionListener(mousecontroller);
    }

    //This class deals with keypresses
    class KeyControl implements KeyListener{
        PlayArea parentScreen;
        
        public KeyControl(PlayArea b){
            super();
            parentScreen=b;
        }
        
        public void keyTyped(KeyEvent e) {}
        
        public void keyPressed(KeyEvent e) {
            int key= e.getKeyCode();
            if(key == Options.up){
                System.out.println("Pressed Up");
                ScreenStack.getTopScreen().keyPressUp();
            }else if(key == Options.down){
                System.out.println("Pressed Down");
                ScreenStack.getTopScreen().keyPressDown();
            }else if(key == Options.left){
                System.out.println("Pressed Left");
                ScreenStack.getTopScreen().keyPressLeft();
            }else if(key == Options.right){
                System.out.println("Pressed Right");
                ScreenStack.getTopScreen().keyPressRight();
            }else if(key == Options.button1){
                System.out.println("Pressed Button 1 - A");
                ScreenStack.getTopScreen().keyPressB1();
            }else if(key == Options.button2){
                System.out.println("Pressed Button 2 - B");
                ScreenStack.getTopScreen().keyPressB2();
            }else if(key == Options.button3){
                System.out.println("Pressed Button 3 - X");
                ScreenStack.getTopScreen().keyPressB3();
            }else if(key == Options.button4){
                System.out.println("Pressed Button 4 - Y");
                ScreenStack.getTopScreen().keyPressB4();
            }else if(key == Options.button5){
                System.out.println("Pressed Button 5 - L");
                ScreenStack.getTopScreen().keyPressB5();
            }else if(key == Options.button6){
                System.out.println("Pressed Button 6 - R");
                ScreenStack.getTopScreen().keyPressB6();
            }else if(key == Options.button7){
                System.out.println("Pressed Button 7 - Start");
                ScreenStack.getTopScreen().keyPressB7();
            }else if(key == Options.button8){
                System.out.println("Pressed Button 8 - Aux 1");
                ScreenStack.getTopScreen().keyPressB8();
            }else if(key == Options.button9){
                System.out.println("Pressed Button 9 - Aux 2");
                ScreenStack.getTopScreen().keyPressB9();
            }else if(key == Options.button10){
                System.out.println("Pressed Button 10 - Aux 3");
                ScreenStack.getTopScreen().keyPressB10();
            }
        }
        
        public void keyReleased(KeyEvent e) {}
    }

    class MouseControl implements MouseInputListener{
        //private final int MENU_SIZE = 23;
        public void mouseClicked(MouseEvent e){
            int x = e.getX() - parentFrame.getInsets().left;
            int y = e.getY() - parentFrame.getInsets().top;
            //int y = e.getY() - parentFrame.getInsets().top - MENU_SIZE;
            
            
            if(e.getButton() == e.BUTTON1){
                //first mouse button
            }else{
                
            }
        }

        public void mouseEntered(MouseEvent e) {}
        
        public void mouseExited(MouseEvent e) {}
        
        public void mousePressed(MouseEvent e) {
            int x = e.getX() - parentFrame.getInsets().left;
            int y = e.getY() - parentFrame.getInsets().top;
            //int y = e.getY() - parentFrame.getInsets().top - MENU_SIZE;
            
            if(e.getButton() == e.BUTTON1){
                
            }else{
                
            }
        }

        public void mouseReleased(MouseEvent e) {
            int x = e.getX() - parentFrame.getInsets().left;
            int y = e.getY() - parentFrame.getInsets().top;
            //int y = e.getY() - parentFrame.getInsets().top - MENU_SIZE;
            
            
            
            if(e.getButton() == e.BUTTON1){
                //first mouse button
            }else{
                
            }
        }
        
        public void mouseDragged(MouseEvent e) {
            int x = e.getX() - parentFrame.getInsets().left;
            int y = e.getY() - parentFrame.getInsets().top;
            //int y = e.getY() - parentFrame.getInsets().top - MENU_SIZE;
            
        }
        
        public void mouseMoved(MouseEvent e) {}
    }
}